package com.wdy.dao.impl;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.wdy.dao.PostDao;
import com.wdy.po.Post;
@SuppressWarnings("unchecked")
public class PostDaoHibernateImpl extends HibernateDaoSupport implements PostDao {
	
	private static final Log log = LogFactory.getLog(PostDaoHibernateImpl.class);
	private static final String DATE = "date";
	
	//hibernate.lock
	
	public void attachClean(Post instance) {
		log.warn("attaching clean Post instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.warn("attach successful");
		} catch (DataAccessException e) {
			log.error("attach failed", e);
			throw e;
		}
	}
	//hibernate.saveOrUpdate
	
	public void attachDirty(Post instance) {
		log.warn("attaching dirty Post instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.warn("attach successful");
		} catch (DataAccessException e) {
			log.error("attach failed", e);
			throw e;
		}
		
	}
	//hibernate.delete
	
	public void delete(Post persistentInstance) {
		log.warn("deleting Post instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.warn("delete successful");
		} catch (DataAccessException e) {
			log.error("delete failed", e);
			throw e;
		}
		
		
	}
	//hibernate.find
	
	public List<Post> findAll() {
		log.warn("finding all");
		try {
			String queryString = "from Post";
			List<Post> result = (List<Post>)getHibernateTemplate().find(queryString);
			log.warn("find all successful,result size: " + result.size());
			return result;
		} catch (DataAccessException e) {
			log.error("find all failed", e);
			throw e;
		}
	}
	//hibernate.findByExample
	
	public List<Post> findByExample(Post instance) {
		log.warn("finding Post instance by example");
		try {
			List<Post> result = (List<Post>)getHibernateTemplate().findByExample(instance);
			log.warn("find Post instance by example successful, result size: " + result.size());
			return result;
		} catch (DataAccessException e) {
			log.error("find Post instance by example failed", e);
			throw e;
		}
	}
	//hibernate.findById

	public Post findById(Integer id) {
		log.warn("finding Post instance by id");
		try {
			Post post = (Post)getHibernateTemplate().get(Post.class, id);
			log.warn("find Post instance by id successful");
			return post;
		} catch (DataAccessException e) {
			log.error("find Post instance by id failed", e);
			throw e;
		}
	}
	//hibernate.find
	
	public List<Post> findByProperty(String propertyName, Object value) {
		log.warn("finding Post instance with property: " + propertyName + ", value: " + value);
		String queryString = "from Post as model where model." + propertyName + "=?";
		try {
			List<Post> result = (List<Post>)getHibernateTemplate().find(queryString, value);
			log.warn("find Post instance by property successful, result size: " + result.size());
			return result;
		} catch (DataAccessException e) {
			log.error("find Post instance by property failed", e);
			throw e;
		}
	}
	//findByProperty
	
	public List<Post> findByDate(String value) {
		return findByProperty(DATE, value);
	}
	//hibernate.merge
	
	public Post merge(Post detachedInstance) {
		log.warn("merging Post instance");
		try {
			Post user = (Post)getHibernateTemplate().merge(detachedInstance);
			log.warn("merge Post successful");
			return user;
		} catch (DataAccessException e) {
			log.error("merge Post failed", e);
			throw e;
		}
	}
	//hibernate.save
	
	public void save(Post transientInstance) {
		log.warn("saving Post instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.warn("save Post instance successful");
		} catch (DataAccessException e) {
			log.error("save Post instance failed", e);
			throw e;
		}
	}

}
